import { Message } from '@element-plus/icons-vue'
import axios from 'axios'

const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_API,
  timeout: 10000,
})

// request 拦截器
service.interceptors.request.use(
  config => {
    // token 验证
    return config
  },
  error => {
    ElMessage.error('请求有误！')
    console.log(error)
    return Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    if (response.data.code === '200') {
      return response.data
    } else {
      ElMessage.error(response.data.message || 'Server Error!')
      return Promise.reject(response.data.message || 'Server Error!')
    }
  },
  error => {
    ElMessage.error('服务异常！')
    console.log(error)
    return Promise.reject(error)
  }
)

export default service